{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.webdriver.support import expected_conditions as EC\n",
    "from selenium.webdriver.support.wait import WebDriverWait\n",
    "import time\n",
    "import datetime\n",
    "from openpyxl import Workbook"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "wb = Workbook()\n",
    "sheet = wb.active\n",
    "sheet.title = '公众号推文'\n",
    "header = ['标题', '时间', '链接']\n",
    "sheet.append(header)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver = webdriver.Chrome()\n",
    "driver.get('https://weixin.sogou.com/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "wait = WebDriverWait(driver, 10)\n",
    "input = wait.until(EC.presence_of_element_located((By.NAME, 'query')))\n",
    "input.send_keys('涛哥聊Python')\n",
    "driver.find_element_by_xpath(\"//input[@class='swz']\").click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "num = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_news():\n",
    "    global num\n",
    "    time.sleep(1)\n",
    "    news_lst = driver.find_elements_by_xpath(\"//li[contains(@id,'sogou_vr_11002601_box')]\")\n",
    "    for news in news_lst:\n",
    "        source = news.find_elements_by_xpath('div[2]/div/a')[0].text\n",
    "        if '涛哥聊Python' not in source:\n",
    "            continue\n",
    "        num += 1\n",
    "        title = news.find_elements_by_xpath('div[2]/h3/a')[0].text\n",
    "        date = news.find_elements_by_xpath('div[2]/div/span')[0].text\n",
    "        if '前' in date:\n",
    "            today = datetime.datetime.today()\n",
    "            if '天' in date:\n",
    "                delta = datetime.timedelta(days=int(date[0]))\n",
    "            elif '小时' in date:\n",
    "                delta = datetime.timedelta(hours=int(date.replace('小时前', ' ')))\n",
    "            else:\n",
    "                delta = datetime.timedelta(minutes=int(date.replace('分钟前', ' ')))\n",
    "            date = str((today - delta).strftime('%Y-%m-%d'))\n",
    "        date = datetime.datetime.strptime(date, '%Y-%m-%d').strftime('%Y-%m-%d')\n",
    "        url = news.find_elements_by_xpath('div[2]/h3/a')[0].get_attribute('href')\n",
    "        print(num, title, date)\n",
    "        print('-' * 10)\n",
    "        row = [title, date, url]\n",
    "        sheet.append(row)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 聊聊涛哥是如何赚到第一个10W的? 2020-05-17\n",
      "----------\n",
      "2 卧槽!现在3K的工作都找不到了,谈下涛哥的看法 2020-06-24\n",
      "----------\n",
      "3 太赞了!2019最新Python官方报告来了 2020-05-30\n",
      "----------\n",
      "4 8个超好用的Python内置函数,提升效率必备! 2020-03-20\n",
      "----------\n",
      "5 大赞!Python官方年度报告来了 2020-05-07\n",
      "----------\n",
      "6 被使用次数最多的22个Python外部模块 2020-07-06\n",
      "----------\n",
      "7 Python最冷门的模块 2020-07-12\n",
      "----------\n",
      "8 中文版!Python入门学习的三件法宝!附免费下载 2020-06-14\n",
      "----------\n",
      "9 利用 Python 爬取了 13966 条运维招聘信息,我得出了哪些结论? 2020-07-13\n",
      "----------\n",
      "10 我用Python分分钟做了个股票分析工具 2020-06-11\n",
      "----------\n",
      "11 微软官方再次上线了Python教程,这次比较高级 2020-05-12\n",
      "----------\n",
      "12 卧槽!终于知道涛哥我为啥赚不到钱了 2020-07-07\n",
      "----------\n",
      "13 Python 3.9 正式版要来了,会有哪些新特性? 2020-06-30\n",
      "----------\n",
      "14 原创|3个学习Python的小技巧 2020-06-29\n",
      "----------\n",
      "15 自从学会这个技巧,涛哥赚了10W+ 2020-05-29\n",
      "----------\n",
      "16 我用 Python 集齐了五福 2020-01-16\n",
      "----------\n",
      "17 微软再出神器,这次终于对Python下手了! 2020-07-13\n",
      "----------\n",
      "18 最受欢迎 12 个 Python 开源框架,你用过哪些? 2020-07-08\n",
      "----------\n",
      "19 下载 | 《Python数据科学速查表中文版》 2020-07-14\n",
      "----------\n",
      "20 如何使用 Python 绘制一套动态图形? 2020-07-13\n",
      "----------\n",
      "21 做死涛哥继续死磕! 2020-05-18\n",
      "----------\n",
      "22 用 Python 做动态二维码哄女友开心 2020-04-12\n",
      "----------\n",
      "23 Python 中各种下划线的骚操作:_、_xx、xx_、__xx、__xx__、_classname_ 2020-03-22\n",
      "----------\n",
      "24 童年的游戏,Python一行代码就能玩 2020-07-06\n",
      "----------\n",
      "25 原创|谈谈涛哥在视频号的收获 2020-07-01\n",
      "----------\n",
      "26 如何用Python把微信这个地球转动? 2020-07-09\n",
      "----------\n",
      "27 整理一波Python 工程师面试题,你能搞定几道? 2020-06-17\n",
      "----------\n",
      "28 PDF课件下载:零基础学Python人工智能 2020-06-24\n",
      "----------\n",
      "29 下载 | 9G火爆的Python爬虫教程+ 520页《图解机器学习》 2020-06-23\n",
      "----------\n",
      "30 原创|Python Web开发没人使用么? 2020-05-31\n",
      "----------\n",
      "31 太赞了!总结一份Python源码学习资料,给有需要的人! 2020-07-10\n",
      "----------\n",
      "32 向Excel说再见,神级编辑器统一表格与Python 2019-08-22\n",
      "----------\n",
      "33 Python 中有 3 个不可思议的返回功能 2020-03-18\n",
      "----------\n",
      "34 我总结的60页《Python之路.pdf》,都是干货! 2020-06-16\n",
      "----------\n",
      "35 教你如何阅读 Python 开源项目代码 2020-01-11\n",
      "----------\n",
      "36 近期股市大热,我们来利用Python做一波股票分析 2020-07-09\n",
      "----------\n",
      "37 plotly | python可视化进阶之路 2020-03-06\n",
      "----------\n",
      "38 4月份最热门的11个Python开源项目——面试、爬虫、机器学习应有尽有! 2020-05-11\n",
      "----------\n",
      "39 聊一聊 Python 中的闭包 2019-11-23\n",
      "----------\n",
      "40 原创|涛哥跪了,删除一时爽,找回火葬场! 2020-05-27\n",
      "----------\n",
      "41 太好玩了,我用Python写了个火影忍者版的连连看 2019-08-28\n",
      "----------\n",
      "42 你可能不知道的 Python 技巧 2020-02-09\n",
      "----------\n",
      "43 30 个 Python 的最佳实践、小贴士和技巧 2020-01-12\n",
      "----------\n",
      "44 Python 绘图还在用 Matplotlib?发现一款手绘可视化神器! 2019-12-25\n",
      "----------\n",
      "45 Python 操作 Excel 哪家强? 2020-03-30\n",
      "----------\n",
      "46 Python3.8 了解的差不多了吧,Python3.9 新特性了解一下! 2019-11-03\n",
      "----------\n",
      "47 充气娃娃什么感觉?Python告诉你 2019-08-19\n",
      "----------\n",
      "48 全面深入理解 Python 面向对象 2019-12-10\n",
      "----------\n",
      "49 涛哥的读者交流群 2019-11-23\n",
      "----------\n",
      "50 Python在计算内存时应该注意的问题? 2020-03-17\n",
      "----------\n",
      "51 你必须掌握的20个python代码,短小精悍,用处无穷 2020-03-10\n",
      "----------\n",
      "52 涛哥知识星球及极客时间各种专栏返现 2019-02-15\n",
      "----------\n",
      "53 详解Python 3.8的海象算子:大幅提高程序执行效率 2020-01-30\n",
      "----------\n",
      "54 Python 技巧 | 让函数返回结果的7个建议 2019-07-04\n",
      "----------\n",
      "55 「知识清单」Python必备的69个函数,你掌握了吗? 2019-02-27\n",
      "----------\n",
      "56 巧用 Matplotlib 动画,让你的 Python 可视化大放异彩 2019-10-23\n",
      "----------\n",
      "57 原创|聊聊我的大学之路 2020-05-26\n",
      "----------\n",
      "58 深入探讨 Python 的 import 机制:实现远程导入模块 2019-11-17\n",
      "----------\n",
      "59 在Win上做Python开发?当然是用官方的MS Terminal和VS Code了 2019-08-27\n",
      "----------\n",
      "60 Docker 容器化部署 Python 应用 2019-08-23\n",
      "----------\n",
      "61 541页的《利用Python进行数据分析》(附电子书和源码下载) 2020-06-25\n",
      "----------\n",
      "62 这款神器,能把 Python 代码执行过程看地一清二楚 2019-10-29\n",
      "----------\n",
      "63 Python资源大全 2019-01-21\n",
      "----------\n",
      "64 为什么说Python工程师,一定要学数据库? 2020-07-01\n",
      "----------\n",
      "65 Python批量下载无版权图片 2019-10-22\n",
      "----------\n",
      "66 吐血整理 | 14G 最适合入门的Python+AI学习教程资料,免费送给你~ 2019-11-06\n",
      "----------\n",
      "67 Python GUI开发,效率提升10倍的方法! 2019-10-14\n",
      "----------\n",
      "68 聊聊程序员的能力矩阵 2020-06-01\n",
      "----------\n",
      "69 Python 3.8 一周后发布,这几个特性值得关注 2019-10-08\n",
      "----------\n",
      "70 Python奇淫技巧之自动登录哔哩哔哩 2020-01-06\n",
      "----------\n",
      "71 终极利器|一篇文章讲清楚Python虚拟环境 2019-01-25\n",
      "----------\n",
      "72 谷歌出品|推出了史上最强的Python在线编辑器 2019-05-06\n",
      "----------\n",
      "73 抢票神器成GitHub热榜第一,支持候补抢票,Python跑起来 | 标星8400 2019-09-11\n",
      "----------\n",
      "74 北大开源了Python中文分词工具包,准确度远超Jieba 2019-10-17\n",
      "----------\n",
      "75 列举几个用Python月入3W大佬的公众号 2019-10-27\n",
      "----------\n",
      "76 Python有趣|数据分析三板斧 2019-03-12\n",
      "----------\n",
      "77 销量过亿的106页 《Python进阶》中文版 | 下载 2020-06-22\n",
      "----------\n",
      "78 原创|100天输出计划过去了1/4,简单聊聊 2020-05-30\n",
      "----------\n",
      "79 再见 Python,Hello Julia! 2020-06-16\n",
      "----------\n",
      "80 7个Python性能优化工具,值得推荐 2020-02-29\n",
      "----------\n"
     ]
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    get_news()\n",
    "    if i == 9:\n",
    "        break\n",
    "    driver.find_element_by_id(\"sogou_next\").click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.find_element_by_name('top_login').click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "while True:\n",
    "    try:\n",
    "        next_page = driver.find_element_by_id(\"sogou_next\")\n",
    "        break\n",
    "    except:\n",
    "        time.sleep(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "next_page.click()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "81 7个Python性能优化工具,值得推荐 2020-02-29\n",
      "----------\n",
      "82 2019必学的10大顶级Python库! 2019-11-11\n",
      "----------\n",
      "83 非常全的通俗易懂 Python 魔法方法指南(下) 2020-02-11\n",
      "----------\n",
      "84 强烈推荐一个我佩服的Python爬虫工程师 2019-10-19\n",
      "----------\n",
      "85 用Python偷偷告诉你国庆8亿人都去哪儿浪? 2019-10-03\n",
      "----------\n",
      "86 墙裂推荐!B站上的Python学习资源 2020-03-03\n",
      "----------\n",
      "87 5700亿,16227注!15年来,双色球一等奖中奖占比率几乎不变(附代码,中奖人) 2019-03-11\n",
      "----------\n",
      "88 5个顶级异步Python框架,你别说你一个都没见过 2020-01-05\n",
      "----------\n",
      "89 推荐一些做Python数据分析的数据集 2019-05-13\n",
      "----------\n",
      "90 python爬取招聘网站数据,利用Tableau可视化交互大屏,指导你如何学习、找工作! 2020-03-23\n",
      "----------\n",
      "91 今天聊聊Python中的装饰器,保证你满意! 2019-11-05\n",
      "----------\n",
      "92 [成都]知道创宇招聘 Python开发&运维工程师 2019-04-24\n",
      "----------\n",
      "93 你常常看到的 __init__.py 到底是个啥? 2020-04-28\n",
      "----------\n",
      "94 牛逼了| 从0到1 搭建Web性能监控系统 2019-07-12\n",
      "----------\n",
      "95 再谈视频号 2020-06-22\n",
      "----------\n",
      "96 曲线救国:提高github下载速度到2MB/s 2020-06-22\n",
      "----------\n",
      "97 2018年最后一次送书活动,26本技术书籍过新年! 2018-12-26\n",
      "----------\n",
      "98 第9次赠书,35本技术书籍,包邮送! 2019-04-03\n",
      "----------\n",
      "99 推荐几个Github中国区排名前100的公众号 2020-06-06\n",
      "----------\n",
      "100 第7次赠书,25本经典书籍免费送,速来! 2019-03-18\n",
      "----------\n",
      "101 原创|5分钟建立反脆弱系统 2020-06-06\n",
      "----------\n",
      "102 第4次赠书,包邮送20本金典书籍,赶紧来! 2019-02-20\n",
      "----------\n",
      "103 第5次赠书,无套路包邮送14本书,赶紧来! 2019-03-05\n",
      "----------\n",
      "104 推荐几个Github Star过万的大佬公众号 2020-06-20\n",
      "----------\n",
      "105 雷军,扎克伯格,Linus 等巨佬的办公桌 2020-06-24\n",
      "----------\n",
      "106 会了这两个Flask练手项目,毕设、接私活不用愁 2020-07-12\n",
      "----------\n",
      "107 已拿阿里巴巴算法offer,感谢这几个德才兼备的博士们的公众号 2019-10-26\n",
      "----------\n",
      "108 他被称为＂中国第一程序员＂,一人之力单挑微软 2020-07-12\n",
      "----------\n",
      "109 最强神器工具:新一代多系统启动 U 盘装机解决方案 2020-07-08\n",
      "----------\n",
      "110 2019年第一次赠书,9本书籍免费送! 2019-01-09\n",
      "----------\n",
      "111 欢迎朋友们投稿(规则&福利) 2019-05-09\n",
      "----------\n",
      "112 原创|远离那些无关的人 2020-05-25\n",
      "----------\n",
      "113 原创|Github这些牛逼的点你用起来了么? 2020-07-04\n",
      "----------\n",
      "114 这个只有1.5M的软件,能让你的网速快3倍 2020-07-04\n",
      "----------\n",
      "115 资源 | 工程师必备!最好的九张机器学习深度学习代码速查表 2020-07-02\n",
      "----------\n",
      "116 这个NLP工具,玩得根本停不下来 2020-07-02\n",
      "----------\n",
      "117 腾讯月薪20k的数据分析人才,需要具备怎样的技能? 2020-07-03\n",
      "----------\n",
      "118 推荐几个月入3w的公众号大佬 2020-07-06\n",
      "----------\n",
      "119 我总结的60页《Python之路.pdf》,都是干货! 2020-06-21\n",
      "----------\n"
     ]
    }
   ],
   "source": [
    "while True:\n",
    "    get_news()\n",
    "    try:\n",
    "        driver.find_element_by_id(\"sogou_next\").click()\n",
    "    except:\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.quit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "wb.save('涛哥聊Python.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
